import { doLogin } from "@/api/login";
import { setItem, getItem } from "@/utils/storage";
import { TOKEN, USERINFO } from "@/contanst";


export default {
    namespaced: true,
    state: () => ({
        token: getItem(TOKEN) || '',
        userInfo: getItem(USERINFO) || '',
    }) ,
    mutations: {
        saveToken(state, token){
            state.token = token;
            setItem(TOKEN, token)
        }
    },
    actions: {
        login({commit}, loginForm){
            return new Promise((resolve, reject) => {
                let { username, password } = loginForm
                doLogin(username, password ).then((res)=>{
                    console.log(res);
                    commit('saveToken', res.token)
                    resolve(res)
                }).catch(error=>{
                    reject(error)
                })
            })
        }
    },
}